package com.baijing.springbootstarterdata.web;

import com.baijing.springbootstarterdata.dao.PersonMongoRepository;
import com.baijing.springbootstarterdata.domain.Location;
import com.baijing.springbootstarterdata.domain.PersonMongo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.rmi.server.LoaderHandler;

import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;

@RestController
public class DataMongoController {
    @Autowired
    private PersonMongoRepository personMongoRepository;

    @RequestMapping("/save")
    public PersonMongo save() {
        PersonMongo personMongo = new PersonMongo("baijing", 18);
        Collection<Location> locations = new LinkedHashSet<>();
        Location loc1 = new Location("北京", "2012");
        Location loc2 = new Location("上海", "2013");
        locations.add(loc1);
        locations.add(loc2);
        personMongo.setLocations(locations);
        return personMongoRepository.save(personMongo);
    }

    @RequestMapping("/q1")
    public PersonMongo q1(String name) {
        return personMongoRepository.findByName(name);
    }

    @RequestMapping("/q2")
    public List<PersonMongo> q2(Integer age) {
        return personMongoRepository.withQueryFindByAge(age);
    }

}
